package study07;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Java03_Reflect_test {
    public static void main(String[] args) throws Exception {
//        反射 - 练习
        Class<Emp> empClass = Emp.class;
        Constructor<Emp> declaredConstructor = empClass.getDeclaredConstructor();

        Emp emp = declaredConstructor.newInstance(); // 实例化

//        获取对象属性
        Field account = empClass.getField("account");
        Field password = empClass.getField("password");

//        给属性赋值
        account.set(emp, "admin");
        password.set(emp, "admin");

//        获取方法
        Method login = empClass.getMethod("login");

//        调用方法
        Object result = login.invoke(emp);
        System.out.println(result);
    }
}

class Emp {
    public String account;
    public String password;
    public boolean login() {
        if ("admin".equals(account) && "admin".equals(password)) {
            return true;
        } else {
            return false;
        }
    }
}
